package org.wingstudio.controller.admin;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.wingstudio.entity.Link;
import org.wingstudio.entity.PageBean;
import org.wingstudio.service.LinkService;
import org.wingstudio.util.ResponseUtil;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 链接管理模块
 * Created by liao on 16-11-16.
 */

@Controller
@RequestMapping("/user/admin")
public class LinkController {

    @Resource
    private LinkService linkService;
    @RequestMapping("saveLink")
    public String saveLink(Link link,
                           HttpServletResponse response,
                           HttpServletRequest request)
            throws Exception{
        ServletContext application= RequestContextUtils.getWebApplicationContext(request).getServletContext();
        linkService.add(link);
        List<Link> linkList=linkService.list();
        application.setAttribute("linkList",linkList);
        JSONObject result=new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }

    @RequestMapping("/link")
    public ModelAndView setLink()throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        List<Link> linkList=linkService.list();
        modelAndView.addObject("linkList",linkList);
        modelAndView.setViewName("user/admin/system/linkManage");
        return modelAndView;
    }


    @RequestMapping("/listLink")
    public String listLink(@RequestParam(value="page",required=false)String page,
                           @RequestParam(value="rows",required=false)String rows,
                           HttpServletResponse response)
            throws Exception{
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());

        List<Link> newsList=linkService.list();
        Long total=linkService.getTotal(map);
        JSONObject result=new JSONObject();
        JsonConfig jsonConfig=new JsonConfig();
        //jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray=JSONArray.fromObject(newsList,jsonConfig);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 通过id查找实体
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/findLinkById")
    public String findLinkById(@RequestParam(value="id")String id,HttpServletResponse response)
            throws Exception{
        Link link=linkService.findLinkById(Integer.parseInt(id));
        JSONObject jsonObject=JSONObject.fromObject(link);
        ResponseUtil.write(response, jsonObject);
        return null;
    }

    /**
     * 修改链接
     */
    @RequestMapping("/updateLink")
    public String updateLink(@RequestParam(value="id")String id,Link link,
                             HttpServletResponse response)
            throws Exception{

        Map<String,Object> map=new HashMap<>();
        map.put("id",Integer.parseInt(id));
        map.put("link",link.getLink());
        map.put("linkName",link.getLinkName());
        linkService.updateLink(map);
        JSONObject result=new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 删除链接
     */
    @RequestMapping("/deleteLink")
    public String deleteLink(@RequestParam(value="id")String id
            ,HttpServletResponse response,
                             HttpServletRequest request)throws Exception{
        linkService.deleteLink(Integer.parseInt(id));
        ServletContext application= RequestContextUtils.getWebApplicationContext(request).getServletContext();
        List<Link> linkList=linkService.list();
        application.setAttribute("linkList",linkList);
        JSONObject result=new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }
}
